singleApp.controller('configurationCtrl', ['$http', '$scope', '$sce', function ($http, $scope, $sce) {
    $scope.list = [];

    //Init
    $scope.init = function () {
        layer.load(2);
        var api_url = window.singleConfig.rootUrl + "/api/admin/configurationService.php?method=getAll";
        $http.get(api_url).success(function (data) {
            $scope.list = data.data;
            layer.closeAll();
        });
    };

    $scope.init();

    $scope.save = function () {
        var api_url = window.singleConfig.rootUrl + '/api/admin/configurationService.php';
        var postdata = {};
        angular.forEach($scope.list, function (data, index, array) {
            postdata[data.option_key] = data.option_value;
        });
        postdata["method"] = "update";

        layer.load(2);
        $http({
            method: 'POST',
            url: api_url,
            data: $.param(postdata),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        })
		.success(function (data) {
		    if (data.code == "success") {
		        layer.closeAll();
		        layer.msg("Configuration is saved.");
		    }
		    else {
		        layer.closeAll();
		        layer.msg(data.message);
		    }
		}).error(function (data) {
		    layer.closeAll();
		    layer.msg("Error to save the record.");
		});
    };
}]);